/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import Pojo.Contract;
import Pojo.Employees;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author knyazev_o
 */
public class DaoContract implements Interface.InterfaceContract{

    @Override
    public boolean register(Session session, Contract contract) throws Exception {
        session.save(contract);        
        return true;
    }
    
    @Override
    public List<Contract> getAll(Session session) throws Exception {
        String hql="from Contract";
        Query query=session.createQuery(hql);        
        List<Contract> listEmployees =(List<Contract>) query.list();        
        return listEmployees;
    }
    
    @Override
    public List<Contract> getAllMy(Session session, Long employees_id) throws Exception {
        String hql="from Contract where Contract.employee_id="+employees_id;
        Query query=session.createQuery(hql);        
        List<Contract> listEmployees =(List<Contract>) query.list();        
        return listEmployees;
    }

    @Override
    public Contract getByIdContract(Session session, Long id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String getNumberYear(Session session) throws Exception {
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
        String year = yearFormat.format(new Date());
        
        session.createSQLQuery("CREATE SEQUENCE IF NOT EXISTS "
                + "CONTRACT.SEQ_NUMBER_CONTRACT START WITH 1 INCREMENT BY 1 "
                + "NO CACHE").executeUpdate();        
        
        return year+" - "+session.createSQLQuery(
               "select NEXTVAL('CONTRACT.SEQ_NUMBER_CONTRACT') as num from dual");
    }

        
}
